package com.atguigu.mybatisplusdemo.controller;

import com.atguigu.mybatisplusdemo.beans.Employee;
import com.atguigu.mybatisplusdemo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmployeeController {


    @Autowired
    private EmployeeService service;

    //处理用户对单个员工的增删改查
    @RequestMapping("/emp")
    public Object handle(String op, Integer id, String lastname,String gender,String email){
        //先把用户的参数封装为数据模型
        Employee employee = new Employee(id, lastname, gender, email,null);

        //调用Service业务模型处理
        switch (op){
            //isBlank(str):str是null 或 '' 或 '白字符（tab、回车、空格）'，返回true
            case "select":if (id == null){
                return "id非法，请传入id!";
            }else {
                Employee e = service.getById(id);
                return e == null ? "查无此人" : e;
            }

            case "delete":if (id == null){
                return "id非法，请传入id!";
            }else {
                service.removeById(id);
                return "ok";
            }

            case "update":if (id == null){
                return "id非法，请传入id!";
            }else {
                service.updateById(employee);
                return "ok";
            }

            case "insert":{
                service.save(employee);
                return "ok";
            }
        }
        return "ok";
    }


    @RequestMapping("/getAllEmp")
    public Object handle1(){
        return service.list();
    }

}


